﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using Telligent.DynamicConfiguration.Components;

namespace Whitesmith.GoogleAdsWidget
{
    public class Main : ConfigurableContentFragmentBase
    {
        #region ContentFragmentBase Methods

        public override void AddContentControls(System.Web.UI.Control control)
        {
            // Rendering the Content of the Widget.
            this.RenderContent(control);
        }

        public override string FragmentDescription
        {
            get 
            {
                return "Displays the Google Ads.";
            }
        }

        public override string FragmentName
        {
            get
            {
                return "Google AdSense";
            }
        }

        public override PropertyGroup[] GetPropertyGroups()
        {
            PropertyGroup googleAdsOptGroup = new PropertyGroup("GoogleAdsGroup", "Google Ads Options", 0);
            Property propertyTitle = new Property("propertyTitle", "Widget Title:", PropertyType.String, 0, "Google Ads Widget");
            Property propertyGoogleAdsClientID = new Property("propertyGoogleAdsClientID", "Google Ads Publisher ID:", PropertyType.String, 1, "");
            Property propertyGoogleAdsUnitID = new Property("propertyGoogleAdsUnitID", "Google Ads Unit ID:", PropertyType.String, 2, "");
            Property propertyGoogleAdsSize = new Property("propertyGoogleAdsSize", "Google Ads Unit Size (ex. 468x60):", PropertyType.String, 3, "");

            googleAdsOptGroup.Properties.Add(propertyTitle);
            googleAdsOptGroup.Properties.Add(propertyGoogleAdsClientID);
            googleAdsOptGroup.Properties.Add(propertyGoogleAdsUnitID);
            googleAdsOptGroup.Properties.Add(propertyGoogleAdsSize);

            return new PropertyGroup[] { googleAdsOptGroup };
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Renders the currenly configured Content on the Widget Body.
        /// </summary>
        private void RenderContent(System.Web.UI.Control control)
        {
            string[] adUnitSize = this.GetStringValue("propertyGoogleAdsSize", "").Split('x');
            
            // Adding the configured code into Script Tag
            HtmlGenericControl userAdScript = new HtmlGenericControl("script");
            userAdScript.Attributes.Add("type", "text/javascript");
            userAdScript.InnerHtml = String.Format("google_ad_client = '{0}'; google_ad_slot = '{1}'; google_ad_width = {2}; google_ad_height = {3};", this.GetStringValue("propertyGoogleAdsClientID", ""), this.GetStringValue("propertyGoogleAdsUnitID", ""), adUnitSize[0], adUnitSize[1]);

            // Adding the standard Script Tag
            HtmlGenericControl standardAdScript = new HtmlGenericControl("script");
            standardAdScript.Attributes.Add("type", "text/javascript");
            standardAdScript.Attributes.Add("src", "http://pagead2.googlesyndication.com/pagead/show_ads.js");

            control.Controls.Add(userAdScript);
            control.Controls.Add(standardAdScript);
        }

        /// <summary>
        /// Renders Title of the Widget, as already configured.
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public override string GetFragmentHeader(System.Web.UI.Control control)
        {
            return this.GetStringValue("propertyTitle", "Google Ads");
        }

        #endregion
    }
}
